package com.zwface.demo.function.login;

import android.os.Bundle;
import android.view.View;

import com.zwface.demo.R;
import com.zwface.demo.base.BaseActivity;
import com.zwface.demo.comm.comm;
import com.zwface.demo.databinding.ActivityResetPwdBinding;
import com.zwface.demo.http.HttpCallback;
import com.zwface.demo.http.ZWAsyncHttpClient;
import com.zwface.demo.utils.CountDownTimerUtils;
import com.zwface.demo.utils.FastjsonHelper;

import java.util.HashMap;
import java.util.Map;


public class ResetPwdActivity extends BaseActivity<ActivityResetPwdBinding> implements View.OnClickListener {

    @Override
    public int onLayoutId() {
        return R.layout.activity_reset_pwd;
    }

    @Override
    public void onInitData(Bundle savedInstanceState) {
        super.onInitData(savedInstanceState);
    }

    @Override
    public void onInitListener() {
        super.onInitListener();
        binding.tvLogin.setOnClickListener(this);
        binding.btnConfirm.setOnClickListener(this);
        binding.btnCode.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId())
        {
            case R.id.tv_login:
                finish();
                break;
            case R.id.btn_confirm:
                resetPwd();
                break;
            case R.id.btn_code:
                getSmsCode();
                break;
        }
    }

    /**
     * 获取短信验证码
     */
    private void getSmsCode()
    {
        String account = binding.etAccount.getText().toString();
        if(!comm.ValidationForm(account,"手机号不能为空"))return;
        String url = comm.API_GET_SMS_CODE;
        Map<String,String> map = new HashMap<>();
        Map<String,String> map1 = new HashMap<>();
        map1.put("mobile",account);
        map.put("sendSmsParam", FastjsonHelper.serialize(map1));
        ZWAsyncHttpClient.post(url, map1, new HttpCallback() {
            @Override
            public void OnSuccess(int i, String res) {
                showToast("发送成功");
                CountDownTimerUtils countDownTimerUtils = new CountDownTimerUtils(context,binding.btnCode,60000,1000);
                countDownTimerUtils.start();
            }

            @Override
            public void OnFailure(int i, String res) {
                showToast(res);
            }
        });
    }

    /**
     * 找回密码
     */
    private void resetPwd()
    {
        String account = binding.etAccount.getText().toString();
        String code = binding.etCode.getText().toString();
        String pwd = binding.etPwd.getText().toString();
        if(!comm.ValidationForm(account,"手机号不能为空"))return;
        if(!comm.ValidationForm(code,"验证码不能为空"))return;
        if(!comm.ValidationForm(pwd,"密码不能为空"))return;
        Map<String,String> map = new HashMap<>();
        map.put("telephone",account);
        map.put("validateCode",code);
        map.put("password",pwd);
        showLoading("请稍后...");
        ZWAsyncHttpClient.post(comm.API_FORGET_PWD, map, new HttpCallback() {
            @Override
            public void OnSuccess(int i, String res) {
                dismissLoading();
                showToast(res);
                finish();
            }

            @Override
            public void OnFailure(int i, String res) {
                dismissLoading();
                showToast(res);
            }
        });
    }
}
